<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$live_id=intval($_RQ['id']);
	if($live_id){
		$this->session('live_id',$live_id);
	}
}
$live_id=$this->session('live_id');
$wxapp_live=pdo_get('core_account_wxapp_live',array('uniacid'=>$_SESSION['uniacid'],'id'=>$live_id));
$wxapp = pdo_get('core_account_wxapp',array('id'=>$wxapp_live['wxapp_id']),array('title','plugin_name'));
$tabs=array(
	array('action'=>'/activity.live.index/','title'=>'返回小程序列表'),
	array('op'=>'list','title'=>$wxapp['title'].'-活动列表'),
	array('op'=>'edit','title'=>'添加活动'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'live_id'=>$live_id,'ishuodong'=>1);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['roomsid']){
		$params['roomsid']=$_RQ['roomsid'];
		$return['params']['roomsid']=$_RQ['roomsid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$total='';
	$list=pdo_getslice('core_account_wxapp_live_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		$goods_ids=array();
		foreach($list as &$item){
			if($item['status']==1 && $item['appgoodsid']){//审核中的活动
				$goods_ids[]=$item['appgoodsid'];
			}
		}
		if($goods_ids){
			$wxapp_id=pdo_getcolumn('core_account_wxapp_live',array('id'=>$live_id),'wxapp_id');
			$res=model('core/wxapp_api',$wxapp_id)->status_live_goods($goods_ids);
			if($res['errcode']==0){
				foreach($res['goods'] as &$rv){
					if($rv['audit_status']){
						pdo_update('core_account_wxapp_live_goods',array('status'=>$rv['audit_status']),array('appgoodsid'=>$rv['goods_id']));
					}
				}
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list',
			'tip'=>'本活动是保存在本地的，先创建本地活动，再发布到小程序直播！',
			'edit_op'=>'edit','delall'=>false,'del_op'=>'del','list'=>array(
			array('type'=>'image','title'=>'活动图片','name'=>'image'),
			array('type'=>'string','title'=>'活动名称','name'=>'title'),
			array('type'=>'string','title'=>'所属商户','name'=>'merchname'),
			array('type'=>'switch','title'=>'开启奖励？','name'=>'huo_open','set'=>'set'),
			array('type'=>'radio','title'=>'活动类型','name'=>'huo_type','options'=>'huo_type'),
			array('type'=>'radio','title'=>'状态','name'=>'status','options'=>'status'),
			array('type'=>'post','title'=>'发布活动到直播','name'=>'','op'=>'setgoodstolive','show'=>array('status'=>array(0))),
			array('type'=>'post','title'=>'重新提交审核','name'=>'','op'=>'auditgoodstolive','show'=>array('status'=>array(3))),
		),'action'=>array(
			array('type'=>'post','op'=>'onsale0','title'=>'下架','show'=>array('xiajia'=>0)),
			array('type'=>'post','op'=>'onsale1','title'=>'上架','show'=>array('xiajia'=>1)),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'活动名称'),
		)
	);
	$return['htmls']=$htmls;
	$return['huo_type']=array('看直播奖励积分');
	$return['status']=array('未发布','审核中','已审核','审核不通过');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['title'] || !$post['image']){
			exi('请填写标题和图片！','error');
		}
		$data=array(
			'image'=>trim($post['image']),
			'title'=>trim($post['title']),
			'merchid'=>trim($post['merchid']),
			'price'=>0.01,
			'originalprice'=>intval($post['originalprice'])?:10,
			'huo_open'=>trim($post['huo_open']),
			'huo_type'=>intval($post['huo_type']),
			'huo_set'=>ijson_encode($post['huo_set']),
		);
		$id=intval($post['id']);
		if(!$id){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['live_id']=$live_id;
			$data['createtime']=TIMESTAMP;
			$data['ishuodong']=1;
			pdo_insert('core_account_wxapp_live_goods',$data);
			exi('添加成功！','','goback');
		}else{
			if($post['status']==2 && $post['appgoodsid']){
				$goods=array(
					'price2'=>$post['price'],
					'price'=>intval($post['originalprice']?:$post['price']*1.2),
					'priceType'=>3,
					'url'=>'jy_weishop/goods/detail/index?id='.$post['goods_id'],
					'goodsId'=>$post['appgoodsid'],
				);
				$wxapp_id=pdo_getcolumn('core_account_wxapp_live',array('id'=>$post['live_id']),'wxapp_id');
				$res=model('core/wxapp_api',$wxapp_id)->up_live_goods($goods);
			}elseif($post['status']==3 && $post['appgoodsid']){
				$wxapp_id=pdo_getcolumn('core_account_wxapp_live',array('id'=>$post['live_id']),'wxapp_id');
				$goods=array(
					'coverImgUrl'=>model('core/wxapp_api',$wxapp_id)->upload_media($post['image'],'image'),
					'name'=>$post['title'],
					'priceType'=>3,
					'price2'=>$post['price'],
					'price'=>intval($post['originalprice']?:$post['price']*1.2),
					'url'=>'jy_weishop/goods/detail/index?id='.$post['goods_id'],
					'goodsId'=>$post['appgoodsid'],
				);
				$res=model('core/wxapp_api',$wxapp_id)->up_live_goods($goods);
			}
			pdo_update('core_account_wxapp_live_goods',$data,array('id'=>$id));
			exi('修改成功！','','goback');
		}
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_wxapp_live_goods',array('id'=>$id));
	}else{
		$item=array('status'=>0);
	}
	$item['huo_set']=json_decode($item['huo_set'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'直播活动商品',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'tip','title'=>'<red>活动发布到直播间，在直播间是展示在商品列表里面的，客户需要点击商品，才能参与领取活动奖励；审核中的活动不允许修改，未审核的活动允许修改所有</red>'),
			array('type'=>'string','title'=>'活动标题','name'=>'title','help'=>'活动名称，最长14个汉字，1个汉字相当于2个字符','show'=>array('status'=>array(0,3))),
			array('type'=>'image','title'=>'活动图片','name'=>'image','help'=>'<red>图片规则：图片尺寸最大300像素*300像素；否则无法发布到直播间</red>','show'=>array('status'=>array(0,3))),
			array('type'=>'string','title'=>'奖励价值','name'=>'originalprice','show'=>array('status'=>array(0,2,3))),
			array('type'=>'switch','title'=>'是否开启？','name'=>'huo_open'),
			array('type'=>'radio','title'=>'活动类型','name'=>'huo_type','options'=>'huo_type'),
			array('type'=>'array','name'=>'huo_set','title'=>'奖励积分阶梯','add_tip'=>'添加一个阶梯',
				'list'=>array(
					array('type'=>'unitstring','unit'=>'分钟','name'=>'fen','title'=>'看直播N分钟'),
					array('type'=>'unitstring','unit'=>'积分','name'=>'num','title'=>'奖励N个积分'),
				),
					'details'=>array(
					array('type'=>'unitstring','unit'=>'分钟','name'=>'fen','title'=>'看直播N分钟'),
					array('type'=>'unitstring','unit'=>'积分','name'=>'num','title'=>'奖励N个积分'),
				)
			),
		)
	);
	$return['huo_type']=array('看直播奖励积分');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['huo_open'])){
		$update['huo_open']=intval($post['huo_open']);
	}
	if($update){
		pdo_update('core_account_wxapp_live_goods',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='setgoodstolive'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要发布的商品！','error');
	}
	$live_id=$this->session('live_id');
	$goods=pdo_get('core_account_wxapp_live_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$wxapp_id=pdo_getcolumn('core_account_wxapp_live',array('id'=>$live_id,'uniacid'=>$_SESSION['uniacid']),'wxapp_id');
	$res=model('core/wxapp_api',$wxapp_id)->add_live_goods($goods);
	if($res['errcode']==0){
		$data=array(
				'appgoodsid'=>$res['goodsId'],
				'auditid'=>$res['auditId'],
				'status'=>1,
		);
		pdo_update('core_account_wxapp_live_goods',$data,array('id'=>$id));
		exi('发布成功！');
	}else{
		exi('发布失败！','error');
	}
	
}
if($_RQ['op']=='auditgoodstolive'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要发布的商品！','error');
	}
	$live_id=$this->session('live_id');
	$goods=pdo_get('core_account_wxapp_live_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$wxapp_id=pdo_getcolumn('core_account_wxapp_live',array('id'=>$live_id),'wxapp_id');
	$res=model('core/wxapp_api',$wxapp_id)->audit_live_goods($goods['appgoodsid']);
	if($res['errcode']==0){
		$data=array(
				'auditid'=>$res['auditId'],
				'status'=>1,
		);
		pdo_update('core_account_wxapp_live_goods',$data,array('id'=>$id));
		exi('提交成功！');
	}else{
		exi('提交失败！','error');
	}
}
if($_RQ['op']=='setgoodstorooms'){
	$id=intval($_RQ['id']);
	$live_id=$this->session('live_id');
	if($_RQ['postdata']){
		$post=get_postdata();
		$goods_ids=array();
		foreach($post['goods'] as $v){
			$goods_ids[]=$v['appgoodsid'];
		}
		$wxapp_id=pdo_getcolumn('core_account_wxapp_live',array('id'=>$live_id),'wxapp_id');
		$res=model('core/wxapp_api',$wxapp_id)->add_live_goodstorooms($goods_ids,$post['roomid']);
		$data=array(
			'goods'=>ijson_encode($post['goods']),
		);
		pdo_update('core_account_wxapp_live_rooms',$data,array('id'=>$post['id']));
		exi('导入成功','','goback');
	}
	if(!$id){
		exi('请先选择直播间！','error');
	}
	$return['item']=pdo_get('core_account_wxapp_live_rooms',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$return['item']['goods']=json_decode($return['item']['goods'],true);
	foreach($return['item']['goods'] as $k=>$gv){
		if(!$gv['ishuodong']){
			unset($return['item']['goods'][$k]);
		}
	}
	if(!$return['item']['roomid']){
		exi('该直播间还没创建到直播平台！','error');
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'导入活动商品进直播间',
		'edit'=>array(
			array('type'=>'tip','title'=>'只有发布并审核成功的活动商品，才能被选择！'),
			array('type'=>'choose_many','name'=>'goods','title'=>'选择要导入的活动商品','action'=>'/activity.live.get_data/get_goods/getinfo=1&ishuodong=1','choose_title'=>'选择活动商品'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	exi('暂时不支持删除！','error');
	exi('删除成功！');
}